package leetcode;

public class LeetCodeMain93 {
    public int maxProfit(int[] prices) {
        int length=prices.length;
        int[][] dp=new int[length][4];
        dp[0][0]=-prices[0];
        for (int i = 1; i < length; i++) {
            dp[i][0]=Math.max(dp[i-1][0],Math.max(dp[i-1][3],dp[i-1][1])-prices[i]);
            dp[i][1]=Math.max(dp[i-1][1],dp[i-1][3]);
            dp[i][2]=dp[i-1][0]+prices[i];
            dp[i][3] = dp[i - 1][2];
        }
        return Math.max(dp[length-1][1],Math.max(dp[length-1][2],dp[length-1][3]));
    }

    String src;
    int ptr=0;
    public String decodeString(String s) {
        src=s;
        return getString();
    }
    public String getString() {
        if (ptr==src.length()||src.charAt(ptr)==']'){
            return "";
        }
        char cur=src.charAt(ptr);
        int repTime=1;
        String ret="";
        if (Character.isDigit(cur)){
            repTime=getDigits();
            ++ptr;
            String str=getString();
            ++ptr;
            while (repTime-->0){
                ret+=str;
            }
        }else if (Character.isLetter(cur)){
            ret=String.valueOf(src.charAt(ptr++));
        }
        return ret+getString();
    }
    public int getDigits() {
        int ret = 0;
        while (ptr < src.length() && Character.isDigit(src.charAt(ptr))) {
            ret = ret * 10 + src.charAt(ptr++) - '0';
        }
        return ret;
    }
}
